package com.itheima.user.controller;

import com.itheima.order.feign.OrderFeign;
import com.itheima.response.UserOrderDTO;
import com.itheima.user.pojo.User;
import com.itheima.user.service.UserService;
import org.dozer.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private Mapper dozerMapper;
    @RequestMapping("/{username}")
    public User findByName(@PathVariable("username") String username){

        return userService.findByName(username);
    }
    //根据用户名查找对应订单
    @GetMapping("/findUserOrders")
    public List<UserOrderDTO> findUserOrders(){
        //根据用户名通过feign调用订单微服务
        String username = "lijialong";
        List<UserOrderDTO> orderByName = orderFeign.findOrderByName(username);

        User user = userService.findByName(username);
        for (UserOrderDTO userOrderDTO : orderByName) {
            BeanUtils.copyProperties(user,userOrderDTO);
        }
        return orderByName;
    }
}
